﻿@using Newtouch.Herp.Infrastructure.Enum;
@using Newtouch.Tools
@{
    ViewBag.Title = "InStorage";
    Layout = "~/Views/Shared/_Index.cshtml";
}
<style>
    .req {
        color: red;
    }
</style>
<form id="form1">
    <div class="panel panel-default">
        <div class="panel-heading">入库信息</div>
        <div style="padding-right:10px;">
            <table class="form">
                <tr>
                    <th class="formTitle">配送单号：</th>
                    <td class="formValue">
                        <input id="deliveryNo" class="form-control" placeholder="配送单号" />
                    </td>
                    <th class="formTitle"><span class="req">*</span>供应商：</th>
                    <td class="formValue">
                        <input id="supplierName" class="form-control" placeholder="名称 拼音" />
                        <input id="supplierId" type="hidden" value="" />
                    </td>
                    <th class="formTitle">入库方式：</th>
                    <td class="formValue formDdlSelectorTd">
                        <select id="ddlRKFS" class="form-control required"></select>
                    </td>
                    <th class="formTitle">入库单号：</th>
                    <td class="formValue">
                        <input id="Pdh" type="text" class="form-control newtouch_Readonly" />
                    </td>
                </tr>
            </table>
        </div>
        <div class="panel-heading">物资明细</div>
        <div id="divYPMX" style="padding-right:10px;">
            <table class="form">
                <tr>
                    <th class="formTitle"><span class="req">*</span>物资：</th>
                    <td class="formValue">
                        <input id="wzmc" type="text" class="form-control" placeholder="拼音 名称 商品名" oninput="cleanProductId()" />
                        <input id="productId" type="hidden" value="" />
                        <input id="lbmc" type="hidden" value="" />
                    </td>
                    <th class="formTitle"><span class="req">*</span>入库数量：</th>
                    <td class="formValue">
                        <div class="row">
                            <div class="col-md-8">
                                <input id="sl" type="text" class="form-control " onchange="RKSL_Leave()" />
                            </div>
                            <div class="col-md-4">
                                <select id="dw" class="form-control " onchange="RKSL_Leave()"></select>
                                <input id="mindwmc" value="" data-zhyz="" data-name="" type="hidden" />
                                <input id="bmdwmc" value="" data-zhyz="" data-name="" type="hidden" />
                            </div>
                        </div>
                    </td>
                    <th class="formTitle"><span class="req">*</span>进价总额：</th>
                    <td class="formValue">
                        <input id="zje" type="text" class="form-control" onchange="JJZE_Leave()" style="width: 70%;float: left;" />
                        <span style="line-height:24px;margin-left: 10px;">(元)</span>
                    </td>
                    <th class="formTitle"><span class="req">*</span>批号：</th>
                    <td class="formValue">
                        <input id="ph" type="text" class="form-control" />
                    </td>
                </tr>
                <tr>
                    <th class="formTitle">发票号：</th>
                    <td class="formValue">
                        <input id="fph" type="text" class="form-control" />
                        <input id="editRowId" value="" type="hidden" />
                    </td>
                    <th class="formTitle"><span class="req">*</span>有效期：</th>
                    <td class="formValue">
                        <input id="Yxq" type="text" class="form-control input-wdatepicker" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd' })" value="@DateTime.Now.AddYears(1).ToString("yyyy-MM-dd")" />
                    </td>
                    <th class="formTitle">生产日期：</th>
                    <td class="formValue">
                        <input id="scrq" type="text" class="form-control input-wdatepicker" value="" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd' });" />
                    </td>
                    <th class="formTitle">备注：</th>
                    <td class="formValue">
                        <input id="remark" type="text" class="form-control" />
                    </td>
                </tr>
                <tr>
                    <th class="formTitle">现有库存：</th>
                    <td class="formValue">
                        <input id="slstr" type="text" class="form-control newtouch_Readonly" />
                        <input id="kykcsl" type="text" hidden="hidden" />
                    </td>
                    <th class="formTitle">规格：</th>
                    <td class="formValue">
                        <input id="gg" type="text" class="form-control newtouch_Readonly" />
                    </td>
                    <th class="formTitle">生产厂家：</th>
                    <td class="formValue">
                        <input id="sccj" type="text" class="form-control newtouch_Readonly" />
                        <input id="lsj" type="hidden" value="" />
                        <input id="minlsj" type="hidden" value="" />
                        <input id="jj" type="hidden" value="" />
                    </td>
                </tr>
            </table>
        </div>
    </div>
</form>
@Html.Partial("_MiddleButtonsView", new Newtouch.HIS.Web.Core.Models.BottomButtonViewModel()
{
    ShowKeyList = new[] { 2, 3, 6 },
    F2Text = "添加",
    F3Text = "修改",
    F6Text = "删除"
})
<div>
    <table id="gridRKMX"></table>
</div>

@Html.Partial("_BottomButtonsView", new Newtouch.HIS.Web.Core.Models.BottomButtonViewModel()
{
    ShowKeyList = new int[] { 7, 8, 9 },
    F7Text = "重置",
    F8Text = "入库",
    F9Text = "暂存"
})
<span style="float: left;line-height: 25px;margin: 8px 5px;">入库进价总额：&nbsp;<span id="txtRKZE"></span> &nbsp;元</span>
<script>
    window.newtouch_globalconfig.f4opions = {
        container: "#divYPMX"
    };
    var $gridRkmx = $("#gridRKMX");
    $(".toolbar").css("width", "70%");
    $(function () {
        //浏览器窗口调整大小时重新加载jqGrid的宽
        $(window).resize(function () {
            window.initLayout("MyTabGrid");
        });
        gridRKMX([]);
        initialRKFS();//加载入库方式
        initialGYS();//注册物资供应商的浮层
        initialDeliveryNO();//注册配送单浮层
        newBill();
        BackspaceGYS();
        InitBackspaceKey("Yxq");
        DockingTemporaryCrkdj();
    });

    //删除清楚供应商
    function BackspaceGYS() {
        $("#supplierName").keydown(function (e) {
            var e = window.event ? window.event : e;
            var keyCode = e.which ? e.which : e.keyCode;
            if (keyCode == 8) {//tab
                $("#supplierName").val("");
                $("#supplierId").val("");
            }
        });
    }

    //对接“我的暂存”跳转
    function DockingTemporaryCrkdj() {
        var shzt = getQueryString("shzt");
        if (shzt == null || shzt==="") return;
        var deliveryNo = getQueryString("deliveryNo");
        var djh = getQueryString("djh");
        var gysId = getQueryString("gysId");
        var gysmc = getQueryString("gysmc");
        var crkfsId = getQueryString("crkfsId");
        fillExistDeliveryInfo(shzt, deliveryNo, djh, gysId, gysmc, crkfsId);
    }

    //填充单据信息
    function fillExistDeliveryInfo(shzt, deliveryNo, djh, gysId, gysmc, crkfsId) {
        if (shzt !== "@EnumAuditState.Temporary.GetDescription()") {
            $.modalMsg("对不起，目前只有暂存单才有追加功能", 'warning', 2000);
            return;
        }
        $("#deliveryNo").val(deliveryNo);
        $("#Pdh").val(djh);
        $("#supplierId").val(gysId);
        $("#supplierName").val(gysmc);
        $("#ddlRKFS").val(crkfsId);
        QueryTemporaryCrkmx($("#deliveryNo").val(), djh);
    }

    //获取url中的参数
    function getQueryString(name) {
        // 匹配目标参数
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var result = window.location.search.substr(1).match(reg); // 对querystring匹配目标参数
        if (result != null) {
            return decodeURIComponent(result[2]);
        } else {
            return null;
        }
    }

    //按backspace键清楚所有
    function InitBackspaceKey(id) {
        $("#" + id).keydown(function (e) {
            var e = window.event ? window.event : e;
            var keyCode = e.which ? e.which : e.keyCode;
            if (keyCode === 8) {//tab
                $("#" + id).val("");
            }
        });
    }

    //新入库
    function newtouch_event_f7() {
        var djmx = $gridRkmx.jqGrid('getRowData_AllLine');
        if (djmx != null && djmx.length > 0) {
            $.modalConfirm("存在未提交的记录，确定生成新出库单吗？",
                function (flag) {
                    if (flag) {
                        newBill();
                    }
                });
        }
    }

    //生成新单据
    function newBill() {
        GetNewRkdh(); //换单号
        $("#supplierId").val("");
        $("#supplierName").val("");
        $("#deliveryNo").val("");
        ClearRKMX();//清空明细
        $gridRkmx.jqGrid("clearGridData");
        $("#txtRKZE").html("");
        //启用入库信息
        var list = new Array();
        //list.push({ id: 'supplierName' });
        list.push({ id: 'ddlRKFS' });
        list.push({ id: 'btn_bottombutton_f2' });
        list.push({ id: 'deliveryNo' });
        readonly(list, 2);
        //禁用按钮
        var list = new Array();
        list.push({ id: 'btn_bottombutton_f3' });
        list.push({ id: 'btn_bottombutton_f6' });
        readonly(list, 1);
    }

    //入库方式
    function initialRKFS() {
        $("#ddlRKFS").bindSelect({
            url: "/Storage/GetStorageIOMode?crkbz=0",
            id: "Id",
            text: "crkfsmc"
        });
    }

    //物资供应商
    function initialGYS() {
        $("#supplierName").newtouchBatchFloatingSelector({
            clickautotrigger: true,
            url: '/Storage/GetSupplierList',
            height: 300,
            width: 300,
            caption: "物资供应商",
            ajaxparameters: function () {
                return "key=" + $("#supplierName").val();
            },
            colModel: [
                { label: 'Id', name: 'Id', hidden: true },
                { label: '供应商', name: 'name', widthratio: 80 }
            ],
            itemdbclickhandler: function ($thistr) {
                $("#supplierId").val($thistr.attr('data-Id'));
                $("#supplierName").val($thistr.attr('data-name'));
            }
        });
    }

    //配送单号
    function initialDeliveryNO() {
        $("#deliveryNo").newtouchBatchFloatingSelector({
            clickautotrigger: true,
            url: '/DeliveryManage/DeliveryQuery/InStorageDeliveryQuery',
            height: 300,
            width: 900,
            caption: "配送单号",
            ajaxparameters: function () {
                return "keyword=" + $("#deliveryNo").val();
            },
            colModel: [
                { label: '配送单号', name: 'deliveryNo', width: 150, align: "center" },
                { label: '单据号', name: 'djh', width: 180, align: "center" },
                {
                    label: '审核状态', name: 'shzt', width: 80, align: 'left', formatter: function (val) {
                        switch (val) {
                        case '@((int)EnumAuditState.Temporary)':
                            return "@EnumAuditState.Temporary.GetDescription()";
                        case '@((int)EnumAuditState.Adopt)':
                            return "@EnumAuditState.Adopt.GetDescription()";
                        case '@((int)EnumAuditState.Cancelled)':
                            return "@EnumAuditState.Cancelled.GetDescription()";
                        case '@((int)EnumAuditState.Refuse)':
                            return "@EnumAuditState.Refuse.GetDescription()";
                        case '@((int)EnumAuditState.Waiting)':
                            return "@EnumAuditState.Waiting.GetDescription()";
                        default:
                            return "";
                        }
                    }
                },
                { label: '供应商名称', name: 'gysmc', width: 180, align: "center" },
                { label: '出入库方式', name: 'crkfsmc', width: 80, align: "center" },
                { label: '创建时间', name: 'CreateTime', width: 130, align: "center", formatter: "date", formatoptions: { srcformat: 'Y-m-d H:i:s', newformat: 'Y-m-d H:i:s' } },
                { label: '创建人', name: 'CreatorCode', width: 80, align: "center" },
                { label: 'gysId', name: 'gysId', hidden: true },
                { label: 'crkfsId', name: 'crkfsId', hidden: true }
            ],
            itemdbclickhandler: function ($thistr) {
                fillExistDeliveryInfo($thistr.attr('data-shzt'), $thistr.attr('data-deliveryNo'), $thistr.attr('data-djh'), $thistr.attr('data-gysId'), $thistr.attr('data-gysmc'), $thistr.attr('data-crkfsId'));
            }
        });
    }

    //根据配送单号获取暂存的单据明细
    function QueryTemporaryCrkmx(deliveryNo, djh) {
        $.najax({
            url: "/BillManage/OutOrInStorageBill/QueryTemporaryCrkmx",
            dataType: "json",
            data: { deliveryNo: deliveryNo, djh: djh },
            type: "POST",
            success: function (data) {
                $gridRkmx.jqGrid("clearGridData");
                $gridRkmx.newtouchLocalDataGrid(null, data);
            }
        });
    }

    //加载入库单号
    function GetNewRkdh() {
        $.najax({
            url: "/Storage/GenerateRkdh?r=" + Math.random(),
            dataType: "json",
            async: false,
            success: function (data) {
                if (data.state === "success") {
                    $("#Pdh").val(data.message);
                } else {
                    if (data.state != null) {
                        $.modalMsg(data.state, 'warning', 1500);
                    } else {
                        $.modalMsg("生成入库单号失败", 'warning',1500);
                    }
                }
            }
        });
    }

    //物资选择
    $("#wzmc").newtouchBatchFloatingSelector({
        clickautotrigger: true,
        height: 300,
        width: 750,
        id: 'selectProduct',
        url: '/StorageManage/Storage/DepartmentStockListQuery',
        ajaxmethod: 'Get',
        inputtextcheck: function () {
            if ($("#supplierName").val().replace(" ", "") === "" || $("#supplierId").val() === "") {   //去掉左右两端空格
                $.modalMsg('请先在浮层中选择供应商', 'warning', 1500);
                return false;
            }
            return true;
        },
        ajaxreqdata: function () {
            var reqData = {};
            reqData.key = $("#wzmc").val();
            return reqData;
        },
        caption: "物资信息",
        colModel: [
            { label: '类别', name: 'lbmc', width: 120 },
            { label: '名称', name: 'name', width: 200, align: "left" },
            { label: '库存', name: 'slstr', width: 100 },
            { label: '规格', name: 'gg', width: 120 },
            {
                label: '零售单价', name: 'bmlsj', width: 90, align: "right",
                formatter: function (cellvalue) {
                    return cellvalue ? parseFloat(cellvalue).toFixed(4) : "0.00";
                }
            },
            { label: '单位', name: 'bmdwmc', width: 50 },
            { label: 'Id', name: 'Id', hidden: true },
            { label: 'kykcsl', name: 'kykcsl', hidden: true },
            { label: 'zhyz', name: 'zhyz', hidden: true },
            { label: 'bmdwId', name: 'bmdwId', hidden: true },
            { label: 'zxdwId', name: 'zxdwId', hidden: true },
            { label: 'supplierName', name: 'supplierName', hidden: true },
            { label: 'minlsj', name: 'minlsj', hidden: true },
            { label: 'mindwmc', name: 'mindwmc', hidden: true }
        ],
        itemdbclickhandler: function ($thistr) {
            $("#wzmc").val($thistr.attr('data-name'));
            $("#productId").val($thistr.attr('data-Id'));
            $("#dw").empty();
            if ($thistr.attr('data-bmdwmc') !== $thistr.attr('data-mindwmc')) {
                $("#dw").append("<option data-zhyz='1' value='" + $thistr.attr('data-zxdwId') + "'>" + $thistr.attr('data-mindwmc') + "</option>");   //出库单位
            }
            $("#dw").append("<option data-zhyz='" + $thistr.attr('data-zhyz') + "' value='" + $thistr.attr('data-bmdwId') + "'>" + $thistr.attr('data-bmdwmc') + "</option>");   //出库单位
            $("#mindwmc").val($thistr.attr('data-zxdwId')).attr("data-zhyz", 1).attr("data-name", $thistr.attr('data-mindwmc'));
            $("#bmdwmc").val($thistr.attr('data-bmdwId')).attr("data-zhyz", $thistr.attr('data-zhyz')).attr("data-name", $thistr.attr('data-bmdwmc'));
            $("#slstr").val($thistr.attr('data-slstr'));
            $("#kykcsl").val($thistr.attr('data-kykcsl'));
            $("#gg").val($thistr.attr('data-gg'));
            $("#sccj").val($thistr.attr('data-supplierName'));
            $("#minlsj").val($thistr.attr('data-minlsj'));
            $("#lsj").val(parseFloat($thistr.attr('data-bmlsj')).toFixed(4));
            $("#lbmc").val($thistr.attr('data-lbmc'));
            $("#sl").val("");
            $("#zje").val("");
            $("#dw").trigger("change");

            //禁用入库信息
            var list = new Array();
            list.push({ id: 'deliveryNo' });
            //list.push({ id: 'supplierName' });
            list.push({ id: 'ddlRKFS' });
            readonly(list, 1);
            return;
        }
    });

    //添加按钮
    function newtouch_event_f2() {
        if (!DataValate()) {
            return false;
        }
        var data = getEditRowData();//获取明细
        if (data === false) return false;
        //检查重复项
        var isRepeatedAdd = false;
        $.each($gridRkmx.getDataIDs(), function () {
            var rowData = $gridRkmx.jqGrid('getRowData', this);
            if (rowData.productId === data[0].productId && rowData.ph === data[0].ph && rowData.pc === data[0].pc) {
                isRepeatedAdd = true;
            }
        });
        if (isRepeatedAdd) {
            $.modalAlert("不可重复添加", 'warning');
            return false;
        }
        $gridRkmx.newtouchLocalDataGrid(null, data);
        ComputeRKZE();//计算入库金额
        ClearRKMX();
        return true;
    }

    //获取明细
    function getEditRowData() {
        return [{
            lbmc: $("#lbmc").val(),
            fph: $("#fph").val(),
            wzmc: $("#wzmc").val(),
            gg: $("#gg").val(),
            sl: parseFloat($("#sl").val()),   //最后保存到数据库的数量
            unitName: $("#dw").find("option:selected").text(),
            zhyz: $("#dw").find("option:selected").attr("data-zhyz"),
            ph: $("#ph").val(),
            pc: $("#pc").val(),
            yxq: $("#Yxq").val(),
            lsj: $("#lsj").val(),
            minlsj: $("#minlsj").val(),
            jj: $("#jj").val(),
            zje: $("#zje").val(),
            slstr: $("#slstr").val(),
            remark: $("#remark").val(),
            sccj: $("#sccj").val(),
            scrq: $("#scrq").val(),
            productId: $("#productId").val(),
            rkbmkc: $("#kykcsl").val(),
            zxdwId: $("#mindwmc").val(),
            mindwmc: $("#mindwmc").attr("data-name"),
            zxdwzhyz: $("#mindwmc").attr("data-zhyz"),
            bmdwId: $("#bmdwmc").val(),
            bmdwmc: $("#bmdwmc").attr("data-name"),
            bmdwzhyz: $("#bmdwmc").attr("data-zhyz"),
            unitId: $("#dw").val(),
            ckbm: $("#supplierId").val()
        }];
    }

    //入库明细
    function gridRKMX(mydata) {
        $gridRkmx.newtouchLocalDataGrid({
            height: $(window).height() - document.getElementById('gridRKMX').offsetTop - 85,
            unwritten: false,
            colModel: [
                { label: '物资类型', name: 'lbmc', width: 70, align: 'left' },
                { label: '发票号', name: 'fph', width: 80, align: 'left' },
                { label: '物资名称', name: 'wzmc', width: 110, align: 'left' },
                { label: '规格', name: 'gg', width: 80, align: 'left' },
                { label: '数量', name: 'sl', width: 60, align: 'right' },
                {
                    label: '零售单价', name: 'lsj', width: 70, align: 'right',
                    formatter: function (cellvalue) {
                        return cellvalue ? parseFloat(cellvalue).toFixed(4) : "0.00";
                    }
                },
                {
                    label: '进价单价', name: 'jj', width: 70, align: 'right',
                    formatter: function (cellvalue) {
                        return cellvalue ? parseFloat(cellvalue).toFixed(4) : "0.00";
                    }
                },
                { label: '单位', name: 'unitName', width: 50, align: 'center' },
                { label: '转化因子', name: 'zhyz', width: 60, align: 'center' },
                { label: '批号', name: 'ph', width: 100, align: 'center' },
                { label: '有效期', name: 'yxq', width: 80, align: 'center', formatter: "date", formatoptions: { srcformat: 'Y-m-d', newformat: 'Y-m-d' }},
                {
                    label: '进价金额', name: 'zje', width: 80, align: 'right',
                    formatter: function (cellvalue) {
                        return cellvalue ? parseFloat(cellvalue).toFixed(2) : "0.00";
                    }
                },
                { label: '现有库存', name: 'slstr', width: 70, align: 'right' },
                { label: '备注', name: 'remark', width: 100, align: 'left' },
                { label: '生产厂家', name: 'sccj', width: 150, align: 'left' },
                { label: '生产日期', name: 'scrq', width: 80, align: 'center', formatter: "date", formatoptions: { srcformat: 'Y-m-d', newformat: 'Y-m-d' } },
                { label: 'productId', name: 'productId', hidden: true },
                { label: 'rkbmkc', name: 'rkbmkc', hidden: true },
                { label: 'mindwmc', name: 'mindwmc', hidden: true },
                { label: 'zxdwId', name: 'zxdwId', hidden: true },
                { label: 'zxdwzhyz', name: 'zxdwzhyz', hidden: true },
                { label: 'bmdwId', name: 'bmdwId', hidden: true },
                { label: 'bmdwmc', name: 'bmdwmc', hidden: true },
                { label: 'bmdwzhyz', name: 'bmdwzhyz', hidden: true },
                { label: 'unitId', name: 'unitId', hidden: true },
                { label: 'minlsj', name: 'minlsj', hidden: true },
                { label: 'ckbm', name: 'ckbm', hidden: true }
            ],
            ondblClickRow: function (rowid) {
                gridDbclickRow(rowid);
            }
        }, mydata);
    }

    //双击
    function gridDbclickRow(rowid) {
        if (rowid) {
            var getRowData = $gridRkmx.jqGrid('getRowData', rowid);
            if (getRowData) {
                $("#dw").empty();
                if (getRowData.mindwmc !== getRowData.bmdwmc) {
                    $("#dw").append("<option data-zhyz='1' value='" + getRowData.zxdwId + "'>" + getRowData.mindwmc + "</option>");   //出库单位
                }
                $("#dw").append("<option data-zhyz='" + getRowData.bmdwzhyz + "' value='" + getRowData.bmdwId + "'>" + getRowData.bmdwmc + "</option>");   //出库单位
                $("#mindwmc").val(getRowData.zxdwId).attr("data-zhyz", 1).attr("data-name", getRowData.mindwmc);
                $("#bmdwmc").val(getRowData.bmdwId).attr("data-zhyz", getRowData.bmdwzhyz).attr("data-name", getRowData.bmdwmc);
                $("#dw").val(getRowData.unitId).trigger("change");
                $("#lbmc").val(getRowData.lbmc);
                $("#fph").val(getRowData.fph);
                $("#wzmc").val(getRowData.wzmc);
                $("#gg").val(getRowData.gg);
                $("#sl").val(getRowData.sl);
                $("#zhyz").val(getRowData.zhyz);
                $("#ph").val(getRowData.ph);
                $("#pc").val(getRowData.pc);
                $("#Yxq").val(getRowData.yxq);
                $("#lsj").val(getRowData.lsj);
                $("#minlsj").val(getRowData.minlsj);
                $("#jj").val(getRowData.jj);
                $("#zje").val(getRowData.zje);
                $("#slstr").val(getRowData.slstr);
                $("#remark").val(getRowData.remark);
                $("#sccj").val(getRowData.sccj);
                $("#scrq").val(getRowData.scrq);
                $("#productId").val(getRowData.productId);
                $("#kykcsl").val(getRowData.rkbmkc);
                $("#supplierId").val(getRowData.ckbm);
                RemarkEditRowId(rowid)
            }
        }
    }

    //修改按钮
    function newtouch_event_f3() {
        if ($('#editRowId').val()) {
            if (!DataValate()) {//检查明细
                return false;
            }
            var data = getEditRowData();//获取明细
            $gridRkmx.jqGrid("delRowData", $('#editRowId').val()); //先remove掉那一行
            ////检查重复项
            //var isRepeatedAdd = false;
            //$.each($gridRkmx.getDataIDs(), function () {
            //    var rowData = $gridRkmx.jqGrid('getRowData', this);
            //    if (rowData.productId === data[0].productId && rowData.ph === data[0].ph && rowData.pc === data[0].pc) {
            //        isRepeatedAdd = true;
            //    }
            //});
            //if (isRepeatedAdd) {
            //    $.modalAlert("不可重复添加", 'warning');
            //    return false;
            //}
            $gridRkmx.newtouchLocalDataGrid(null, data);
            ComputeRKZE();//计算入库金额
            ClearRKMX();
        }
        else {//获取grid当前选中的那一行
            var seleId = $gridRkmx.jqGrid('getGridParam', 'selrow');
            if (seleId) {
                gridDbclickRow(seleId);//请求修改该行
            }
        }
    }

    //删除
    function newtouch_event_f6() {
        var delRowid = $gridRkmx.jqGrid('getGridParam', 'selrow');
        if (!delRowid) {
            $.modalAlert("未选中行", 'warning');
            return;
        }
        $gridRkmx.jqGrid('delRowData', delRowid);
        ComputeRKZE();//计算入库总额
        ClearRKMX();
    }

    //外部入库
    function newtouch_event_f8() {
        if ($('#loadingPage').is(':visible')) {
            return;
        }
        var crkdj = Object();
        crkdj.auditState = "@((int)EnumAuditState.Waiting)";
        crkdj.Pdh = $("#Pdh").val();
        crkdj.deliveryNo = $("#deliveryNo").val();
        crkdj.crkfs = $("#ddlRKFS").val();
        var djmx = $gridRkmx.jqGrid('getRowData_AllLine');
        if (djmx == null || djmx.length <= 0) {
            $.modalMsg("请先添加入库信息", 'warning', 1500);
            return;
        }
        if (crkdj.deliveryNo.length > 50) {
            $.modalMsg("配送单号不得超过50个字符", 'warning', 1500);
            return;
        }
        if ($("#supplierId").val() === "" || $("#supplierName").val("") === "") {
            $.modalMsg("请选择供应商", 'warning', 1500);
            return;
        }
        crkdj.ckbm = $("#supplierId").val();
        $.najax({
            url: "/StorageManage/Storage/SaveInStorageInfo",
            dataType: "json",
            data: { crkdj: crkdj, crkdjmx: djmx },
            type: "POST",
            success: function (data) {
                if (data.state === 'success') {
                    $.modalMsg("入库成功", 'success', 1000);
                    newBill();//新入库：新单据号 主数据清空 明细清空
                    $gridRkmx.jqGrid("clearGridData");//清空Grid
                }
                else {
                    $.modalAlert(data.message, "error");
                }
            }
        });
    }

    //外部入库 暂存
    function newtouch_event_f9() {
        var crkdj = Object();
        crkdj.auditState ="@((int)EnumAuditState.Temporary)";
        crkdj.Pdh = $("#Pdh").val();
        crkdj.deliveryNo = $("#deliveryNo").val();
        crkdj.crkfs = $("#ddlRKFS").val();
        var djmx = $gridRkmx.jqGrid('getRowData_AllLine');
        if (djmx == null || djmx.length <= 0) {
            $.modalMsg("请先添加入库信息", 'warning', 1500);
            return;
        }
        if (crkdj.deliveryNo.length > 50) {
            $.modalMsg("配送单号不得超过50个字符", 'warning', 1500);
            return;
        }
        if ($("#supplierId").val() === "" || $("#supplierName").val("")==="") {
            $.modalMsg("请选择供应商", 'warning', 1500);
            return;
        }
        crkdj.ckbm = $("#supplierId").val();
        $.najax({
            url: "/StorageManage/Storage/TemporaryStorageInStorageInfo",
            dataType: "json",
            data: { crkdj: crkdj, crkdjmx: djmx },
            type: "POST",
            success: function (data) {
                if (data.state === 'success') {
                    $.modalMsg("入库成功", 'success', 1000);
                    newBill();//新入库：新单据号 主数据清空 明细清空
                    $gridRkmx.jqGrid("clearGridData");//清空Grid
                }
                else {
                    $.modalAlert(data.message, "error");
                }
            }
        });
    }

    //重置
    function newtouch_event_f7() {
        newBill();
    }

    //数量失焦,计算零售金额
    function RKSL_Leave() {
        var minlsj = parseFloat($("#minlsj").val());
        var zhyz = parseFloat($("#dw").find("option:selected").attr("data-zhyz"));
        var lsj = parseFloat(minlsj * zhyz).toFixed(4);
        $("#lsj").val(lsj);

        if ($("#wzmc").val() === "") {
            return false;
        }
        if ($("#sl").val() === "") {
            return false;
        }
        if (isNaN($("#sl").val())) {
            $.modalAlert("数量格式错误", 'warning');
            return false;
        }
        return true;
    }

    //进价总额失焦，计算入库单价
    function JJZE_Leave() {
        if (isNaN($("#zje").val())) {
            $.modalAlert("进价总额格式错误", 'warning');
            return false;
        }
        if ($("#zje").val() === "") {
            return false;
        }
        if ($("#sl").val() === "") {
            return false;
        }
        var sl = parseFloat($("#sl").val());;
        var jjze = parseFloat($("#zje").val());
        var jj = sl === 0 ? 0 : jjze / sl;
        $("#jj").val(parseFloat(jj).toFixed(4));
        return true;
    }

    //计算入库总额
    function ComputeRKZE() {
        var rkze = 0;
        var count = $gridRkmx.getGridParam('records');
        if (count > 0) {
            var rowIds = $gridRkmx.getDataIDs();
            for (var i = 0; i < rowIds.length; i++) {
                var getRowZje = $gridRkmx.getRowData(rowIds[i]).zje;
                rkze = parseFloat(rkze) + parseFloat(getRowZje);
            }
            //入库金额
            $("#txtRKZE").html(rkze.toFixed(2));
        } else {
            $("#txtRKZE").html('');
        }
    }

    //回车加载grid
    $('#remark').keydownEnterEvent(function () {
        if ($('#editRowId').val()) {            //是修改
            newtouch_event_f3();    //模拟 修改按钮 点击
        }
        else {
            newtouch_event_f2();    //模拟 确认按钮 点击
        }
    });

    //数据验证
    function DataValate() {
        if ($("#Pdh").val() === "") {
            $.modalAlert("入库单号生成失败，请刷新页面", 'warning');
            return false;
        }
        if ($("#supplierName").val() === "" || $("#supplierId").val() === "") {
            $.modalAlert("请选择供应商", 'warning');
            return false;
        }
        if ($("#wzmc").val() === "" || $("#productId").val() === "") {
            $.modalAlert("请选择有效的物资", 'warning');
            return false;
        }
        if ($("#sl").val() === "") {
            $.modalAlert("入库数量不能为空", 'warning');
            return false;
        }
        if ($("#zje").val() === "") {
            $.modalAlert("总金额不能为空", 'warning');
            return false;
        }
        if ($("#ph").val() === "") {
            $.modalAlert("批号不能为空", 'warning');
            return false;
        }
        if ($("#Yxq").val() === "") {
            $.modalAlert("有效期不能为空", 'warning');
            return false;
        }
        if ($("#Yxq").val() <= $("#scrq").val().replace(/^\s+|\s+$/g, "")) {
            $.modalAlert("有效期应晚于物资的生产日期", 'warning');
            return false;
        }
        return true;
    }

    //有效期默认加12个月
    function Getyxq(scrq) {
        var s = scrq.split('-');
        var year = Number($.getDate().split('-')[0]) + 1;
        return year + "-" + s[1] + "-" + s[2];
    }

    //清空明细
    function ClearRKMX() {
        $("#sl").val("");
        $("#dw").empty();
        $("#scrq").val("");
        $("#Yxq").val(Getyxq($.getDate()));
        $("#wzmc").val("");
        $("#productId").val("");
        $("#slstr").val("");
        $("#kykcsl").val("");
        $("#gg").val("");
        $("#sccj").val("");
        $("#minlsj").val("");
        $("#lsj").val("");
        $("#jj").val("");
        $("#zje").val("");
        $("#ph").val("");
        cleanEditRowId();
    }

    function cleanProductId() {
        $("#productId").val("");
    }

    //删除保存的当前选中行ID
    function cleanEditRowId() {
        $("#editRowId").val("");
        //启用按钮
        var list = new Array();
        list.push({ id: 'btn_bottombutton_f2' });
        readonly(list, 2);
        //禁用按钮
        var list = new Array();
        list.push({ id: 'btn_bottombutton_f3' });
        list.push({ id: 'btn_bottombutton_f6' });
        readonly(list, 1);
    }

    //记录当前选中行ID
    function RemarkEditRowId(rowId) {
        $("#editRowId").val(rowId);
        //启用按钮
        var list = new Array();
        list.push({ id: 'btn_bottombutton_f3' });
        list.push({ id: 'btn_bottombutton_f6' });
        readonly(list, 2);
        //禁用按钮
        var list = new Array();
        list.push({ id: 'btn_bottombutton_f2' });
        readonly(list, 1);
    }
</script>
